package pub.guoxin.oops;

import io.netty.channel.ChannelFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import pub.guoxin.oops.collector.UdpCollectorProperties;
import pub.guoxin.oops.collector.server.DataGatherServer;

import java.net.InetSocketAddress;

/**
 * Created by guoxin on 17-10-11.
 */
@SpringBootApplication
@EnableAutoConfiguration
public class OopsCollectorApplication implements CommandLineRunner {

    @Autowired
    private DataGatherServer dataGatherServer;

    @Autowired
    private UdpCollectorProperties collectorProperties;

    public static void main(String[] args) {
        SpringApplication.run(OopsCollectorApplication.class, args);
    }

    public void run(String... args) {
        InetSocketAddress address = new InetSocketAddress(collectorProperties.getHost(), collectorProperties.getPort());
        ChannelFuture future = dataGatherServer.start(address);
        Runtime.getRuntime().addShutdownHook(new Thread(() -> dataGatherServer.destroy()));
        future.channel().closeFuture().syncUninterruptibly();
    }

}
